package lc_2352_相等行列对;

import java.util.HashMap;
import java.util.Map;

public class Solution {

    public int equalPairs(int[][] grid) {
        Map<Integer, Integer> cnts = new HashMap<>();
        for (int i = 0; i < grid.length; i++) {
            long h = 0;
            for (int j = 0; j < grid.length; j++) {
                h += grid[i][j];
                h *= 100007;
                h %= 1000000007;
            }
            cnts.merge((int) h, 1, Integer::sum);
        }
        int ans = 0;
        for (int i = 0; i < grid.length; i++) {
            long h = 0;
            for (int j = 0; j < grid.length; j++) {
                h += grid[j][i];
                h *= 100007;
                h %= 1000000007;
            }
            ans += cnts.getOrDefault((int) h, 0);
        }
        return ans;
    }
}